package com.maya.medicalwastes;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.maya.medicalwastes.adapter.CollectListAdapter;
import com.maya.medicalwastes.adapter.FirstMenuAdapter;
import com.maya.medicalwastes.entity.AuthOrgEntity;
import com.maya.medicalwastes.entity.CloseEvent;
import com.maya.medicalwastes.entity.CollectBean;
import com.maya.medicalwastes.entity.KeyBean;
import com.maya.medicalwastes.entity.MenuEntity;
import com.maya.medicalwastes.entity.UserEntity;
import com.maya.medicalwastes.httpNet.ApiService;
import com.maya.medicalwastes.httpNet.HttpService;
import com.maya.medicalwastes.httpNet.callback.ResponseCallBack;
import com.maya.medicalwastes.httpNet.common.SharedPreferencesConstant;
import com.maya.medicalwastes.httpNet.manage.RetrofitManager;
import com.maya.medicalwastes.httpNet.model.ResultMainHomeModel;
import com.maya.medicalwastes.httpNet.model.ResultModel;
import com.maya.medicalwastes.util.MyUtils;
import com.maya.medicalwastes.util.PrefUtils;
import com.maya.medicalwastes.util.StringUtils;
import com.maya.medicalwastes.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

public class MainActivity extends AppCompatActivity {
    /**
     * apk列表
     */
    private ArrayList<MenuEntity> menuList = new ArrayList<>();
    /**
     * menu适配器
     */
    private FirstMenuAdapter apkAdapter;


    @BindView(R.id.rv_apk_list)
    RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initView();
        getWebData();
        keySearch();
        Log.e("111", PrefUtils.getBoolean(MainActivity.this, SharedPreferencesConstant.IS_LOGIN, false) + "");
        Log.e("111111+++++++", PrefUtils.getString(MainActivity.this, SharedPreferencesConstant.TOKEN, " ") + "");

    }

    private void initView() {
        if (apkAdapter == null) {
            apkAdapter = new FirstMenuAdapter(R.layout.item_first_menu, menuList);
        }
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(RecyclerView.HORIZONTAL);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(apkAdapter);
//        PrefUtils.remove(mContext, SharedPreferencesConstant.TOKEN);
//
//        Intent intent =new Intent(mContext, LoginActivity.class);
//        mContext.startActivity(intent);
    }

    public void getWebData() {
        HashMap<String, Object> param = new HashMap<>();
        param.put("menuType", MenuType.FormMobile.getType());
        param.put(ParamConstant.TOKEN, PrefUtils.getString(this, SharedPreferencesConstant.TOKEN, ""));
        Observable<ResultMainHomeModel<UserEntity, MenuEntity, AuthOrgEntity>> call = RetrofitManager.getInstance().getRetrofit().create(ApiService.class).getMenuListCall(param);
        HttpService.getInstance().enqueue(call, new ResponseCallBack<ResultMainHomeModel<UserEntity, MenuEntity, AuthOrgEntity>>() {
            @Override
            public void onResponse(ResultMainHomeModel<UserEntity, MenuEntity, AuthOrgEntity> response) {
                System.out.println(response.getMenus());
                if (response != null && response.httpCode == 200) {
                    getPageData(response);
                } else {
                    //未登录401
                    ToastUtils.ViewToast(MainActivity.this, response.msg);
                }
            }

            @Override
            public void onFailure(int httpCode) {
            }
        }, this);


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(CloseEvent event) {
        MyUtils.IntentToOther(this,LoginActivity.class,null);
        finish();
    }

    public void keySearch() {
        HashMap<String, Object> param = new HashMap<>();
        param.put("key", "CATEGORY");
        param.put(ParamConstant.TOKEN, PrefUtils.getString(this, SharedPreferencesConstant.TOKEN, ""));
        Observable<ResultModel<ArrayList<KeyBean>>> call = RetrofitManager.getInstance().getRetrofit().create(ApiService.class).Keyword(param);
        HttpService.getInstance().enqueue(call, new ResponseCallBack<ResultModel<ArrayList<KeyBean>>>() {
            @Override
            public void onResponse(ResultModel<ArrayList<KeyBean>> response) {
                if (response != null && response.httpCode == 200) {
                    App.getInstance().setWareType(response.data);
//                    for (KeyBean d : response.data) {
//                        if (d.getKey().equals(collectBean.getGarbageCategory())) {
//                            collectBean.setGarKey(d.getValue());
//                            mDialog.add(collectBean);
//                            mDialogListAdapter.notifyDataSetChanged();
//                        }
//                    }
                }
            }

            @Override
            public void onFailure(int httpCode) {
            }
        }, this);
    }


    public void getPageData(ResultMainHomeModel<UserEntity, MenuEntity, AuthOrgEntity> response) {
        App.getInstance().setUserEntity(response.getUser());
        menuList = response.getMenus();
        if (menuList != null && menuList.size() > 0) {
            ArrayList<MenuEntity> tempList = new ArrayList<>();
            tempList.addAll(menuList);
            menuList.clear();
            for (MenuEntity menuEntity : tempList) {
                //一级菜单
                MenuEntity firstMenus = new MenuEntity(menuEntity.getIconcls(), menuEntity.getMenuName());
                if (menuEntity.getMenuBeans() != null && menuEntity.getMenuBeans().size() > 0) {
                    //二级菜单
                    ArrayList<MenuEntity> secondMenus = new ArrayList<>();

                    ArrayList<MenuEntity> menuBeans = menuEntity.getMenuBeans();
                    for (MenuEntity menuItemEntity : menuBeans) {
                        if (menuItemEntity.getMenuType() == 1) {
                            if (StringUtils.isEmpty(menuItemEntity.getRequest())) {
                                continue;
                            }
                            if (menuItemEntity.getRequest().toLowerCase().contains("fragment") || menuItemEntity.getRequest().toLowerCase().contains("activity")) {
                                Map<String, MenuEntity> menuMap = new HashMap<>();
                                MenuEntity entity = menuMap.get(menuItemEntity.getRequest().toLowerCase());
                                if (entity != null) {
                                    secondMenus.add(entity);
                                }
                            } else {
                                secondMenus.add(new MenuEntity(menuItemEntity.getRequest(), menuItemEntity.getIconcls(), menuItemEntity.getMenuName()));
                            }
                        }
                    }

                    if (secondMenus.size() > 0) {
                        firstMenus.setMenuBeans(secondMenus);
                        menuList.add(firstMenus);
                    }
                }
            }

            apkAdapter.replaceData(menuList);

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }
}
